package com.sky.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService  {

  @Autowired
  private CategoryMapper categoryMapper;

  /* 
   * 分类查询
   */
  @Override
  public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'page'");
   PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
   String name = categoryPageQueryDTO.getName();
   Integer type = categoryPageQueryDTO.getType();
   List<Category> list = categoryMapper.page(name, type);
   Page<Category> page = (Page<Category>) list;
   PageResult pageResult = new PageResult();
   pageResult.setTotal(page.getTotal());
   pageResult.setRecords(page.getResult());
   return pageResult;
  }

  /* 
   * 新增分类
   */
  @Override
  public void save(CategoryDTO categoryDTO) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'save'");
   Category category = new Category();
   BeanUtils.copyProperties(categoryDTO, category);
   category.setCreateTime(LocalDateTime.now());
   category.setUpdateTime(LocalDateTime.now());
   category.setCreateUser(BaseContext.getCurrentId());
   category.setUpdateUser(BaseContext.getCurrentId());
   category.setStatus(1);
   categoryMapper.add(category);  
  }

  /* 
   * 分类查询
   */
  @Override
  public List<Category> list(Integer type) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'list'");
   return categoryMapper.list(type);
  }

  /* 
   * 根据id删除分类
   */
  @Override
  public void delete(Long id) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'delete'");
   categoryMapper.delete(id);
  }

  /* 
   * 是否禁用
   */
  @Override
  public void status(String status, String id) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'status'");
   categoryMapper.status(status, id);
  }

  /* 
   * 修改分类
   */
  @Override
  public void update(CategoryDTO categoryDTO) {
    // TODO Auto-generated method stub
   // throw new UnsupportedOperationException("Unimplemented method 'update'");
   categoryMapper.update(categoryDTO);
  }
  
}
